#!/bin/bash

Current_dir=$(pwd)

echo Please select_type : 1、 Soft_IOC 2、Stream_IOC 3、S7nodave_IOC 4、Modbus_IOC
read -p ">>  " select_type
read -p "Please enter IOC_File_Name >> " IOC_FILE_NAME

mkdir -p /opt/epics/local_ioc/$IOC_FILE_NAME
cd /opt/epics/local_ioc/$IOC_FILE_NAME

if [ $select_type -eq 1 ];
then
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -t example myioc
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -i -t example myioc
make
echo Create IOC OK
cp -f -R $Current_dir/db /opt/epics/local_ioc/$IOC_FILE_NAME
echo \# epicsEnvSet\(\"EPICS_CAS_SERVER_PORT\",\"5064\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# epicsEnvSet\(\"EPICS_CAS_REPEATER_PORT\",\"5065\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
cd iocBoot/myioc
chmod +x st.cmd
fi

if [ $select_type -eq 2 ];
then
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -t example myioc
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -i -t example myioc
echo ASYN=/opt/epics/modules/asyn-R4-38 >> ./configure/RELEASE
echo STREAM=/opt/epics/modules/StreamDevice-2.8.18 >> ./configure/RELEASE
cp -f $Current_dir/StreamDevice/Makefile ./myiocApp/src
cp -f $Current_dir/StreamDevice/xxxAppInclude.dbd ./myiocApp/src
make
echo Create StreamDevice IOC OK
cp -f -R $Current_dir/db /opt/epics/local_ioc/$IOC_FILE_NAME
echo \# epicsEnvSet \"STREAM_PROTOCOL_PATH\",\".\" >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# drvAsynIPPortConfigure \"port\",\"192.168.122.111\" >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# asynSetTraceIOMask \"port\",0,4 >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# asynSetTraceMask \"port\",0,9 >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# epicsEnvSet\(\"EPICS_CAS_SERVER_PORT\",\"5064\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# epicsEnvSet\(\"EPICS_CAS_REPEATER_PORT\",\"5065\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
cd iocBoot/myioc
chmod +x st.cmd
fi

if [ $select_type -eq 3 ];
then
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -t example myioc
/opt/epics/base-3.15.9/bin/linux-x86_64/makeBaseApp.pl -i -t example myioc
echo ASYN=/opt/epics/modules/asyn-R4-38 >> ./configure/RELEASE
echo S7NODAVE=/opt/epics/modules/s7nodave-3.0.2 >> ./configure/RELEASE
cp -f $Current_dir/S7nodave/Makefile ./myiocApp/src

make
echo Create S7nodave IOC OK
cp -f -R $Current_dir/db /opt/epics/local_ioc/$IOC_FILE_NAME
echo \# s7nodaveConfigureIsoTcpPort\(\"myPLC\", \"myplc.example.com\", 0, 0, 0\) >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# s7nodaveConfigurePollGroup\(\"myPLC\", \"1s\", 1.0, 0\) >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# asynSetTraceIOMask \"port\",0,4 >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# asynSetTraceMask \"port\",0,9 >> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# epicsEnvSet\(\"EPICS_CAS_SERVER_PORT\",\"5064\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
echo \# epicsEnvSet\(\"EPICS_CAS_REPEATER_PORT\",\"5065\"\)>> /opt/epics/local_ioc/$IOC_FILE_NAME/iocBoot/myioc/st.cmd
cd iocBoot/myioc
chmod +x st.cmd
fi

if [ $select_type -eq 4 ];
then
tar -zxf $Current_dir/Modbus-R2-9.tar.gz -C /opt/epics/local_ioc/$IOC_FILE_NAME
make
echo Create Bodbus IOC OK
fi



